//index.js
const app = getApp();
const qiniuUploader = require("../../utils/qiniuUploader.js"); //七牛sdk
const server = require("../../server.config.js"); //获取上传令牌token

let uploadURL = "https://up.qbox.me";
let domain = "http://oqxfq54dn.bkt.clouddn.com";


Page({
    data: {
        imgs: [],
    },

    onLoad: function () {

    },

    // 点击事件，从本地相册选择图片或使用相机拍照。
    chooseImage() {
        wx.chooseImage({
            success: res => {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                let tempFilePaths = res.tempFilePaths;
                console.log('res=>', res)
                if (this.data.imgs.length + tempFilePaths.length <= 9) {
                    this.data.imgs = [...this.data.imgs, ...tempFilePaths];
                } else {
                    wx.showModal({
                        content: '最多可添加9张图片',
                        showCancel: false,
                        confirmText: "知道了",
                    })
                }
                console.log('imgs=>', this.data.imgs)
                this.setData({
                    imgs: this.data.imgs
                })
            },
        })
    },

    //删除照片,点击删除会有一个图片临时地址参数传进来，在图片数组里删除当前这张就可以
    delect(res) {
        let index = res.currentTarget.dataset.index; //这里带进来的参数是在wxml里自定义的，也就是渲染下标index
        this.data.imgs.splice(index, 1)
        console.log('imgs=>', this.data.imgs)
        this.setData({
            imgs: this.data.imgs,
        });
    },


    //点击查看图片
    previewImage(res) {
        let src = res.target.dataset.src; //点了那张图，就由那张图开始预览
        wx.previewImage({
            current: src, //current = "" 随便用户点那张，都是由第一张开始预览
            urls: this.data.imgs
        })
    },

    //上传图片 
    uploadImageMethod() {
        let reg = /(test)/;
        if (reg.test(server.gain_qntoken)) {
            wx.showModal({
                title: '提示',
                content: '请先把server.config.js里的域名配置了，在做上传操作',
                showCancel: false,
                confirmText: "好的"
            })
        } else {
            wx.showLoading({
                title: '上传中...',
            })
            for (let i = 0; i < imgs.length; i++) {
                let imgName = imgs[i].substr(30, imgs[i].length); // 文件临时路径30-length作为上传“伪文件名”

                //图片给七牛，完成上传操作
                qiniuUploader.upload(imgs[i], res => {
                        console.log('upload =>', res)
                        this.fuzhuMethod(res.imageURL);
                    },
                    (error) => {
                        console.log('error=>', error);
                    }, {
                        uptokenURL: gain_qntoken,
                        uploadURL: uploadURL, //华东
                        key: imgName, // 自定义文件 key
                        domain: domain, //七牛对外下载链接
                        region: 'ECN',
                    });
            }
        }
    },

    //辅助方法,判断所有图片是否上传七牛完成
    fuzhuMethod(imageURL) {
        let checkArray = [];
        checkArray.push(imageURL);
        if (checkArray.length == imgs.length) {
            wx.showToast({
                icon: "success",
                title: "上传成成功！",
                duration: 2000
            })
        }
    },

})